package com.ccb.sc.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ccb.sc.member.convert.WishingBottleConvert;
import com.ccb.sc.common.modules.member.mapper.WishingBottleMapper;
import com.ccb.sc.member.vo.WishingBottleCreateVo;
import com.ccb.sc.member.vo.WishingBottleListVo;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.modules.member.entity.WishingBottle;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author ken
 * @version 1.0
 * @date 2022/8/14 20:25
 */
@Service
public class WishingBottleService extends ServiceImpl<WishingBottleMapper, WishingBottle> {

    public PageVo<WishingBottleListVo> selectWishingBottleList(Long memberId, PageParams queryParam) {
        Page<WishingBottle> page = new Page<WishingBottle>(queryParam.getCurrent(), queryParam.getPageSize());
        Page<WishingBottle> wishingBottlePage = this.page(page, Wrappers.<WishingBottle>lambdaQuery()
                .eq(WishingBottle::getMemberId, memberId)
                .orderByDesc(WishingBottle::getId));
        List<WishingBottle> records = wishingBottlePage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), 0, Lists.newArrayList());
        }
        List<WishingBottleListVo> list = WishingBottleConvert.INSTANCE.entity2ListVo(records);
        return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), wishingBottlePage.getTotal(), list);
    }

    public void create(Long memberId, WishingBottleCreateVo createVo) {
        WishingBottle wishingBottle = WishingBottleConvert.INSTANCE.vo2Entity(createVo);
        wishingBottle.setMemberId(memberId);
        this.save(wishingBottle);
    }
}
